package com.example.test06thread.ReentrantLockTest;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 演示多线程预定电影院座位
 * @author EDZ
 */
public class CinemaBookSeat {

    private static ReentrantLock lock = new ReentrantLock();
    private static void bookSeat(){
        //平常使用得最多的一个方法，就是用来获取锁。如果锁已被其他线程获取，则进行等待。
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"开始预定座位");
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()+"完成预定座位");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    public static void main(String[] args) {
        new Thread(()->bookSeat()).start();
        new Thread(()->bookSeat()).start();
        new Thread(()->bookSeat()).start();
        new Thread(()->bookSeat()).start();
    }
}
